package com.njlime.member.service.sys;

import com.njlime.core.helper.PageBean;
import com.njlime.member.entiies.TField;
import com.njlime.member.entiies.TMember;
import com.njlime.member.repository.sms.FieldDao;
import com.njlime.member.repository.sms.FieldDaoImpl;
import com.njlime.member.repository.sms.MemberDao;
import com.njlime.sys.security.util.UserUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by Dean on 2017-7-27.
 */
@Service
@Transactional(readOnly = true)
public class FieldService {

    @Resource
    private FieldDao fieldDao;


    /**
     * 列表
     *
     * @param pageBean
     * @param field
     * @return
     */
    public PageBean list(PageBean pageBean, TField field) {
        return fieldDao.list(pageBean, field);
    }

    @Transactional(readOnly = false)
    public void addAll(int unitId) {
        try {

            TField field = setField("用户名", 1, 1, "", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_23);
            fieldDao.save(field);
            field.setShort_num(field.getId());


            field = setField("手机号", 1, 1, "用户手机号", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_26);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("昵称", 1, 1, "", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_24);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("真实姓名", 1, 1, "", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_27);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("证件类型", 1, 1, "所登记证件类型", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_1);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("证件号码", 1, 1, "所登记证件号码", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_2);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("账户类型", 1, 1, "用户申请的账户类型", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_3);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("账户号码", 1, 1, "用户所持账户号码", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_4);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("国家地区", 1, 1, "注册时选择国家地区", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_5);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("出生日期", 1, 1, "用户出生日期", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_6);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("性别", 1, 1, "用户性别", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_7);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("个性签名", 1, 1, "个性签名", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_8);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("年龄", 1, 1, "用户年龄", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_9);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("星座", 1, 1, "用户星座", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_10);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("身高", 1, 1, "用户身高", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_11);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("体重", 1, 1, "用户体重", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_12);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("职业", 1, 1, "用户职业", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_13);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("收入", 1, 1, "用户收入", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_14);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("兴趣爱好", 1, 1, "用户兴趣爱好", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_15);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("家乡", 1, 1, "用户家乡", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_16);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("长居地", 1, 1, "用户长居地", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_17);
            fieldDao.save(field);
            field.setShort_num(field.getId());


            field = setField("婚姻状态", 1, 1, "用户婚姻状态", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_18);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("所在地", 1, 1, "用户所在地", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_25);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("个人简介", 1, 1, "用户个人简介", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_19);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("通讯地址", 1, 1, "用户通讯地址", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_20);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("邀请码", 1, 1, "邀请码", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_21);
            fieldDao.save(field);
            field.setShort_num(field.getId());

            field = setField("行业", 1, 1, "用户所属行业", unitId, UserUtils.getUser().getId(),TField.PAGE_KEY_22);
            fieldDao.save(field);
            field.setShort_num(field.getId());
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 设置字段
     *
     * @param fieldName 名称
     * @param filedType 类型
     * @param isMust    是否必须
     * @param remark    备注
     * @param unitId    单位id
     * @param creator   创建人
     * @return
     */
    public TField setField(String fieldName, int filedType, int isMust, String remark, int unitId, int creator,String pageKey) {
        TField field = new TField();
        field.setField_name(fieldName);
        field.setFiled_type(filedType);
        field.setIs_must(isMust);
        field.setRemark(remark);
        field.setStatus(1);
        field.setUnit_id(unitId);
        field.setCreate_time(new Date());
        field.setCreator(creator);
        field.setUpdater(field.getCreator());
        field.setIs_del(0);
        field.setPage_key(pageKey);
        return field;
    }

    /**
     * 修改状态
     * @param info
     * @return
     */
    @Transactional(readOnly = false)
    public int updateStatus(TField info) {
        return fieldDao.updateStatus(  info);
    }

    /**
     * 排序
     * @param id
     * @param type 1上移 2 下移
     * @param unitId 单位id
     * @return
     */
    @Transactional(readOnly = false)
    public int order(int id, int type, int unitId,int userId) {
        TField info = fieldDao.findOne(id);
        if(info.getUnit_id().intValue() == unitId) {
            return fieldDao.order(info, type, unitId,userId);
        }
        return 0;
    }
}
